package com.d3works.studio.web;

import com.d3works.inject.Beans;
import com.d3works.meta.db.MetaJsonField;
import com.d3works.rpc.ActionRequest;
import com.d3works.rpc.ActionResponse;
import com.d3works.studio.db.repo.MetaJsonFieldRepo;
import com.d3works.studio.service.StudioMetaService;

public class MetaJsonFieldController {

  public void trackJsonField(ActionRequest request, ActionResponse response) {
    MetaJsonField metaJsonField = request.getContext().asType(MetaJsonField.class);

    MetaJsonField jsonField =
        Beans.get(MetaJsonFieldRepo.class)
            .all()
            .filter(
                "self.name = ?1 AND self.model = ?2",
                metaJsonField.getName(),
                metaJsonField.getModel())
            .fetchOne();

    if (jsonField != null) {
      return;
    }

    Beans.get(StudioMetaService.class).trackJsonField(metaJsonField);
  }
}
